<?php

class Admin_BlogTagController extends Ext_Controller_Action_Admin
{
    public function init()
    {
        parent::init();
    }
    
    public function showListAction()
    {
        $tags = array();
        $recommendTagAll = App_Model_DbTable_Recommend_Tag::getInstance()->getAll();
        for($i=0;$i<count($recommendTagAll);$i++)
        {
            $recommendTagId = $recommendTagAll[$i]['id'];
            $tagId         = $recommendTagAll[$i]['tagId'];
            
            $tag = App_Model_DbTable_Tags::getInstance()->getRowById($tagId);

            $tags[] = array(
                'recommendTagId' => $recommendTagId,
                'tagId'          => $tagId,
                'url'            => Ext_Url::getInstance()->tag($tagId)->articleList,
                'tagName'        => $tag['tagName'],
            );
        }
        
        $this->view->tags = $tags;
    }
    
    public function saveOrderAction()
    {
        $viewResult = new Ext_View_Result();
        
        $newOrder = $this->_request->getPost('newOrder');
        if($newOrder){
            for($i=0;$i<count($newOrder);$i++)
            {
                $_tagId = $newOrder[$i];
                $row = array(
                    'sequence' => $i,
                );
                App_Model_DbTable_Recommend_Tag::getInstance()->addOld($row, $_tagId);
            }
        }
        
        $viewResult->setResult(1);
        $viewResult->setNoRender();
    }
    
    public function deleteTagAction()
    {
        $viewResult = new Ext_View_Result();
        
        $recommendTagId = $this->filter->filter($this->_request->getPost('recommendTagId'));

        App_Model_DbTable_Recommend_Tag::getInstance()->del($recommendTagId);
        
        $viewResult->addMessage($this->translater->_("Delete success."));
        $viewResult->setResult(1);
        $viewResult->setNoRender();
    }
    
    public function saveTagAction()
    {
        $viewResult = new Ext_View_Result();
        
        $tagName = $this->filter->filter($this->_request->getPost('tagName'));
        
        if(!$tagName){
            $viewResult->addMessage($this->translater->_("Tag name is required."));
            $viewResult->setNoRender();
            return "";
        }
        
        $row = array(
            'tagName'        => $tagName,
            'createTime'     => time(),
        );
        $tagId = App_Model_DbTable_Tags::getInstance()->add($row);

        $recommendTag = App_Model_DbTable_Recommend_Tag::getInstance()->getRowByTagId($tagId);
        if($recommendTag)
        {
            $viewResult->addMessage($this->translater->_("The tag already exists."));
            $viewResult->setNoRender();
            return "";
        }
        
        $sequence = App_Model_DbTable_Recommend_Tag::getInstance()->getMaxSequence();
        $row = array(
            'tagId'     => $tagId,
            'sequence'  => $sequence+1,
        );
        $recommendTagId = App_Model_DbTable_Recommend_Tag::getInstance()->addNew($row);
            
        $recommendTag = App_Model_DbTable_Recommend_Tag::getInstance()->getRowById($recommendTagId);
        
        $tag = array(
            'recommendTagId' => $recommendTagId,
            'tagId'          => $tagId,
            'url'            => Ext_Url::getInstance()->tag($tagId)->articleList,
            'tagName'        => $tagName,
        );
        $this->view->tag = $tag;

        $viewResult->setResult(1);
        $viewResult->addMessage($this->translater->_("Submit success."));
    }
    
}

